<!-- 
  @name: 在地图上标记加注站位置
  @date: 2020.3.17
 -->
<template>
	<view class="content">
		<map
			id="theMap"
			style="width: 750rpx; height: 100vh;"
			show-location
			:latitude="latitude"
			:longitude="longitude"
			:markers="markers"
			@tap="clickMap"
		/>
		
		<view class="bottom_view" v-if="type !== 'det'">
			<button class="btn_submit" @tap="getAdCode">确定</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: '',  //页面类型，默认为添加，若值为 e 只是查看
				markers: [],  //标记点
				latitude: 31.839850,  //位置纬度
				longitude: 117.209004,  //位置经度
			}
		},
		onLoad(e) {
			this.type = e.type;
			if(e.latitude) {
				this.showMark(e);
				this.latitude = e.latitude;
				this.longitude = e.longitude;
			} else {
				this.getLocation();
			}
		},
		methods: {
			//点击地图
			clickMap(e) {
				if(this.type !== 'det') {
					let data = {};
					data.id = 1;
					data.latitude = e.detail.latitude;
					data.longitude = e.detail.longitude;
					data.iconPath = this.$tmp.IMG_SITE;
					data.width = 55;
					data.height = 63;
					this.markers = [];
					this.markers.push(data);
				}
			},
			//通过地图api获取区域行政编码
			getAdCode() {
				if(this.markers.length === 0) {
					this.$showToast('请选择地理位置！');
					return;
				}
				
				uni.showLoading({
					mask: true,
					title: '加载中...'
				});
				
				let data = this.markers[0];
				
				uni.request({
					url: this.$api.amapUrl,
					data: {
						key: this.$tmp.amapKey,
						location: data.longitude + ',' + data.latitude,
						extensions: 'all',
						s: 'rsx',
						sdkversion: 'sdkversion',
						logversion: 'logversion'
					},
					success: (res) => {
						uni.$emit('site_chooseMark', {
							latitude: data.latitude,
							longitude: data.longitude,
							areaCode: res.data.regeocode.addressComponent.adcode
						});
						uni.hideLoading();
						uni.navigateBack({ delta: 1 });
					},
					fail: () => {
						uni.hideLoading();
						this.$showToast('获取地理信息失败，请重试！');
					}
				});
			},
			//详情页显示标记点
			showMark(e) {
				let data = {};
				data.id = 1;
				data.latitude = e.latitude;
				data.longitude = e.longitude;
				data.iconPath = this.$tmp.IMG_SITE;
				data.width = 55;
				data.height = 63;
				this.markers = [];
				this.markers.push(data);
			},
			//获取当前位置
			getLocation() {
				uni.getLocation({
				    type: 'gcj02',
				    success: (res) => {
						this.latitude = res.latitude;
						this.longitude = res.longitude;
				    },
					fail: () => {
						uni.showModal({
						    content: '获取位置信息失败，请检查是否打开位置授权',
							showCancel: false,
						    success: (rs) => {
						        if (rs.confirm) {
									//#ifdef MP-WEIXIN
						            wx.openSetting({});
									//#endif
						        }
						    }
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	
</style>
